/**
 * 
 */
package com.sys.console.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.sys.console.business.SysModuleService;
import com.sys.console.business.SysRoleAuthorityService;
import com.sys.console.business.SysRoleService;
import com.sys.console.cookie.Constant_Cookie;
import com.sys.console.mapper.Sys_Role_AuthorityMapper;
import com.sys.console.pojo.MeunAuthorityTreeJsonDto;
import com.sys.console.pojo.Sys_Module;
import com.sys.console.pojo.Sys_Role;
import com.sys.console.pojo.Sys_Role_Authority_Key;
import com.sys.console.pojo.Sys_User;
import com.sys.object.common.ObjException;
import com.sys.object.common.ObjResultCode;
import com.sys.object.common.ObjResultException;
import com.sys.object.common.ObjResultJSON;

/**
 * @author HS
 *
 */
@Controller
public class SysRoleAuthorityAction {

	@Autowired SysRoleService sysRoleService;
	@Autowired SysModuleService sysModuleService;
	@Autowired SysRoleAuthorityService sysRoleAuthorityService;
	@Autowired Sys_Role_AuthorityMapper  sysRoleAuthorityMapper;
	
	@RequestMapping(value = "/sysRoleAuthority.do", method = RequestMethod.GET)
	public String sysRoleAuthority(HttpServletRequest request,Model model) {
	
		Sys_User currentUser = (Sys_User)request.getSession().getAttribute(Constant_Cookie.CURRENT_USER_SESSION);  
		System.out.println("当前登录的用户为[" + currentUser.getUsername() + "]");  
        request.setAttribute("currUser", currentUser.getUsername());  
		
		try {
			
			ArrayList<Sys_Role> rolelist = sysRoleService.selectByList();
			model.addAttribute("rolelist", rolelist);
			
			Sys_Module record = new Sys_Module();
			record.setPid(0);
			ArrayList<Sys_Module> moduletypelist = sysModuleService.selectByPid(record);
			model.addAttribute("moduletypelist", moduletypelist);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "/system/roleauthority/list";
	}
	
	
	/**
	 * 模块的权限列表
	 */
	@RequestMapping("/sysRoleAuthorityAjaxList.do")
	@ResponseBody
	public ObjResultJSON<ArrayList<Sys_Role_Authority_Key>> sysRoleAuthorityAjaxList(HttpServletRequest request,HttpServletResponse response, Model model,Integer rid, Integer mid) {
		
		ObjResultException<String> ret = new ObjResultException<String>();
		ObjResultJSON<ArrayList<Sys_Role_Authority_Key>> retjson = new  ObjResultJSON<ArrayList<Sys_Role_Authority_Key>>();
		try {
			Sys_Role_Authority_Key key = new Sys_Role_Authority_Key();
			key.setRid(rid);
			key.setMid(mid);
			ArrayList<Sys_Role_Authority_Key> list = sysRoleAuthorityMapper.selectByRidAndMid(key);
			
			if(list!=null&&list.size()>0){
				retjson.setData(list);
				retjson.setResultCode(ObjResultCode.OK);
			}else{
				retjson.setResultCode(ObjResultCode.DATA_ERROR);
			}
		} catch (Exception e) {
			if (e.getClass().equals(ObjException.class)) {
				ret.setResultCode(((ObjException) e).getCode());
			} else {
				ret.setResultCode(ObjResultCode.USER_LOGIN_ERROR);
			}
			model.addAttribute("ret", ret);
		}
		return retjson;
	}
	

	/**
	 * 权限受权
	 * 
	 * @param record
	 * @return
	 */
	@RequestMapping("/addSysRoleAuthority.do")
	@ResponseBody
	public ObjResultException<String> addRoleAuthority(Integer rid, String list) {
		ObjResultException<String> ret = new ObjResultException<String>();
		List<Sys_Role_Authority_Key> listkey = new ArrayList<Sys_Role_Authority_Key>();
		boolean status = true;
		try {
			if (StringUtils.hasText(list)) {
				String[] authority = list.split("#"); // list = 10:SYS_ADD,SYS_BROWSE,SYS_EDIT#11:SYS_EDIT#12:SYS_DELETE#25:SYS_DOWNLOAD
				for (int i = 0; i < authority.length; i++) {
					Sys_Role_Authority_Key delRoleAuthority = new Sys_Role_Authority_Key();
					String[] authoritys = authority[i].split(":", -1);
					int mid = Integer.parseInt(authoritys[0]); // 模块ID
					delRoleAuthority.setMid(mid);
					delRoleAuthority.setRid(rid);
					// 删除原来模块权限
					System.err.println("删除原来模块权限");
					System.err.println("删除原来模块权限==>>delRoleAuthority:"+delRoleAuthority);
					int delsuc = sysRoleAuthorityService.deleteSysRoleAuthority(delRoleAuthority);
					String aids = authoritys[1];
					if (StringUtils.hasText(aids)) {
						String[] arrAid = aids.split(",");
						for (int j = 0; j < arrAid.length; j++) {
							Sys_Role_Authority_Key roleAuthority = new Sys_Role_Authority_Key();
							Integer aid = Integer.parseInt(arrAid[j]);
							roleAuthority.setRid(rid);
							roleAuthority.setAid(aid);
							roleAuthority.setMid(mid);
							listkey.add(roleAuthority);
						}
					}
				}
				if (listkey.size() > 0) {
					int success = sysRoleAuthorityService.saveSysRoleAuthorityByList(listkey);
					if (success == 0)
						status = false;
				}
			}
		} catch (Exception e1) {
			e1.printStackTrace();
			status = false;
		}
		if (status) {
			ret.setResultCode(ObjResultCode.OK);
		}
		return ret;
	}
	
	/**
	 * 树结构-权限受权
	 * 
	 * @param record
	 * @return
	 */
	@RequestMapping("/saveAllotRoleAuthorityTreeJson.do")
	@ResponseBody
	public ObjResultJSON<String> saveAllotRoleAuthorityTreeJson(@RequestBody MeunAuthorityTreeJsonDto dto) {
		ObjResultJSON<String> ret = new ObjResultJSON<String>();
		try {
			if (StringUtils.hasText(dto.getId())) {
				int success = 0;
				success = sysRoleAuthorityService.saveSysRoleAuthorityByTree(dto);
				if(success>0){
					ret.setResultCode(ObjResultCode.OK);
				}else{
					ret.setResultCode(ObjResultCode.SAVE_ERROR);
				}
			}
		} catch (Exception e1) {
			e1.printStackTrace();
			ret.setResultCode(ObjResultCode.ERROR);
		}
		return ret;
	}
}
